<?php

namespace app\service\im;

use app\service\im\Base;

class Group extends Base
{
    /**
     * Describe:创建群组
     * $name 群名称
     * GroupId 自定义群组id
     *Introduction 群简介，最长240字节，使用 UTF-8 编码，1个汉字占3个字节
     * FaceUrl  群头像 URL，最长100字节
     * Notification 群公告，最长300字节，使用 UTF-8 编码，1个汉字占3个字节
     * Type 群组形态，包括 Public（陌生人社交群），Private（即 Work，好友工作群），ChatRoom（即 Meeting，会议群），AVChatRoom（直播群），Community（社群）
     * $ownerAccount 群主id
     * User: TANGFEN
     */
    public function createGroup($ownerAccount, $type, $name, $MemberList = [], $Introduction = '', $FaceUrl = '', $Notification = '')
    {
        if ($type == 1) {
            $type = 'Public';//陌生人社交群
            $url = $this->url . 'group_open_http_svc/create_group';
            $data = [
                'Type' => $type,
                'Owner_Account' => $ownerAccount,
                'Name' => $name,
                'Introduction' => $Introduction,
                'FaceUrl' => $FaceUrl,
                'Notification' => $Notification,
                'MemberList' => $MemberList
            ];
        } else {
            $type = 'AVChatRoom';//直播群
            $url = $this->url . 'group_open_http_svc/create_group';
            $data = [
                'Type' => $type,
                'Owner_Account' => $ownerAccount,
                'Name' => $name,
                'Introduction' => $Introduction,
                'FaceUrl' => $FaceUrl,
                'Notification' => $Notification,
//                'MemberList'=>$MemberList   //用户不能直接加入直播群
            ];
        }

        return $this->common($url, $data);
    }


    /**
     * Describe:修改群基础资料
     * $name 群名称
     * GroupId 自定义群组id
     * Introduction 群简介，最长240字节，使用 UTF-8 编码，1个汉字占3个字节
     * FaceUrl  群头像 URL，最长100字节
     * Notification 群公告，最长300字节，使用 UTF-8 编码，1个汉字占3个字节
     * User: TANGFEN
     */
    public function updateGroup($GroupId, $name, $Introduction = '', $FaceUrl = '', $Notification = '')
    {
        $url = $this->url . 'group_open_http_svc/modify_group_base_info';
        $data = [
            'Name' => $name,
            'Introduction' => $Introduction,
            'FaceUrl' => $FaceUrl,
            'Notification' => $Notification,
            'GroupId' => $GroupId,
            'AppDefinedData'=>[
                'type'=>1,
                'data'=>'sdsd'
            ]
        ];
        return $this->common($url, $data);
    }

    /**
     * 增加群成员
     * @param $GroupId
     * @param $name
     * @param string $Member_Account 用户id
     * @return mixed
     */
    public function addGroupMember($GroupId, $Member_Account)
    {
        $url = $this->url . 'group_open_http_svc/add_group_member';
        $data = [
            'MemberList' => $Member_Account,
            'Silence' => 1,
            'GroupId' => $GroupId,
        ];
        return $this->common($url, $data);
    }

    /**
     * 删除群成员
     * @param $GroupId
     * @param $Member_Account
     * @return mixed
     */
    public function delGroupMember($GroupId, $Member_Account)
    {
        $members = explode(',', $Member_Account);
        $url = $this->url . 'group_open_http_svc/delete_group_member';
        $data = [
            'MemberToDel_Account' => $members,
            'GroupId' => $GroupId,
        ];
        return $this->common($url, $data);
    }

    /**
     * 解散群
     * @param $GroupId
     * @param $Member_Account
     * @return mixed
     */
    public function destroyGroupMember($GroupId)
    {
        $url = $this->url . 'group_open_http_svc/destroy_group';
        $data = [
            'GroupId' => $GroupId,
        ];
        return $this->common($url, $data);
    }

    /**
     * Notes:转让群主
     * User: tangfen
     * Date: 2024/1/3
     * Time: 14:20
     * @param $GroupId
     * @return mixed
     */
    public function changeGroupOwner($GroupId, $newHomeowner)
    {
        $url = $this->url . 'group_open_http_svc/change_group_owner';
        $data = [
            'GroupId' => $GroupId,
            'NewOwner_Account' => $newHomeowner,//新群主 ID
        ];
        return $this->common($url, $data);
    }

    /**
     * 单个/批量禁言
     * @param $GroupId 群组id
     * @param $Member_Account 用户id 为4294967295时永久禁言  为0时表示取消禁言
     * @return mixed
     */
    public function forbidSendMsg($GroupId, $Member_Account)
    {
        $url = $this->url . 'group_open_http_svc/forbid_send_msg';
        $data = [
            'GroupId' => $GroupId,
            'Members_Account' => [$Member_Account],
            'ShutUpTime' => '4294967295',//为4294967295时永久禁言  为0时表示取消禁言
        ];
        return $this->common($url, $data);
    }


    /**
     * Notes:获取直播群在线人数
     * User: tangfen
     * Date: 2023/12/23
     * Time: 13:25
     * @param $GroupId
     * @param $Member_Account
     * @return mixed
     */
    public function getOnlineMemberNum($GroupId)
    {
        $url = $this->url . 'group_open_http_svc/get_online_member_num';
        $data = [
            'GroupId' => $GroupId,
        ];
        return $this->common($url, $data);
    }

    /**
     * Notes:获取直播群在线成员列表
     * User: tangfen
     * Date: 2023/12/23
     * Time: 13:27
     * @param $GroupId
     * @return mixed
     */
    public function getOnlineMembers($GroupId)
    {
        $url = $this->url . 'group_open_avchatroom_http_svc/get_members';
        $data = [
            'GroupId' => $GroupId,
            'Timestamp' => 0
        ];
        return $this->common($url, $data);
    }

    /**
     * Notes:添加好友分组
     * User: tangfen
     * Date: 2023/12/23
     * Time: 14:04
     */
    public function addGroupFriend($From_Account, $GroupName, $To_Account)
    {

        $To_Account = explode(',', $To_Account);
        $GroupName = explode(',', $GroupName);

        $url = $this->url . 'sns/group_add';
        $data = [
            'From_Account' => $From_Account,//用户
            'GroupName' => $GroupName,//分组名称
            'To_Account' => $To_Account//被分组的好友
        ];

        return $this->common($url, $data);
    }


}